package com.ostrich.note.ui.note.views;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.blankj.utilcode.util.ToastUtils;
import com.ostrich.note.R;
import com.ostrich.note.base.BaseMvvmActivity;
import com.ostrich.note.databinding.ActivityNoteDetailBinding;
import com.ostrich.note.entity.BaseResponseEntity;
import com.ostrich.note.event.Event;
import com.ostrich.note.ui.note.adapter.NoteDetailImageAdapter;
import com.ostrich.note.ui.note.adapter.NoteTypeEditAdapter;
import com.ostrich.note.ui.note.entity.NoteEntity;
import com.ostrich.note.ui.note.viewmodel.NoteDetailViewModel;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * 笔记详情
 */
public class NoteDetailActivity extends BaseMvvmActivity<ActivityNoteDetailBinding, NoteDetailViewModel> {
    private ArrayList<NoteEntity> mList;
    private int position;
    private NoteDetailImageAdapter mNoteDetailImageAdapter;
    private NoteEntity note;

    @Override
    protected void iniView() {
        mList = (ArrayList<NoteEntity>) getBundle().getSerializable("noteData");
        position = getBundle().getInt("position");
        if(position >= mList.size()){
            note = mList.get(mList.size()-1);
        }else{
            note  = mList.get(position);
        }

        mNoteDetailImageAdapter = new NoteDetailImageAdapter();
        binding.rv.setAdapter(mNoteDetailImageAdapter);
        binding.rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
    }

    @Override
    public int getLayout() {
        return R.layout.activity_note_detail;
    }

    @Override
    public void initData() {
        showInfo();
    }

    private void showInfo() {
        binding.setNote(note);
        if(note.getIs_scrawl() == 0){
            binding.ivCollect.setImageResource(R.mipmap.btn_uncollect);
        }else{
            binding.ivCollect.setImageResource(R.mipmap.btn_collect);
        }
        ArrayList<String> list = new ArrayList<>();
        String thumb = note.getThumb();
        String[] split = thumb.split(",");
        for (String s : split) {
            list.add(s);
        }
        mNoteDetailImageAdapter.setData(list);

    }

    @Override
    public void initListener() {
        binding.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                binding.ivEdit.setVisibility(View.INVISIBLE);
                binding.tvConfirm.setVisibility(View.VISIBLE);
                binding.tvContent.setFocusable(true);
                binding.tvContent.setFocusableInTouchMode(true);
                binding.tvContent.requestFocus();

                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(binding.tvContent, InputMethodManager.SHOW_IMPLICIT);

            }
        });
        binding.tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = binding.tvContent.getText().toString();
                //String thumb,String title ,long create_time, String content,int scrawl,int type_id,int note_type_id
                viewModel.subclassDataEdit(note.getThumb(),note.getTitle(),note.getNewlyadd(),s,note.getIs_scrawl(),note.getNotepad_id(),note.getNotepad_type_id()).observe(NoteDetailActivity.this, new Observer<BaseResponseEntity>() {
                    @Override
                    public void onChanged(BaseResponseEntity baseResponseEntity) {
                        if(baseResponseEntity.getCode() == 200){
                            ToastUtils.showShort("修改成功");
                            finish();
                            Event event = new Event();
                            event.data = position;
                            event.data2 = note.getTitle();
                            event.data3 = s;

                            event.type = Event.NOTE_UPDATE;
                            event.target = "NoteFragment";
                            EventBus.getDefault().post(event);
                        }
                    }
                });
            }
        });


        binding.clBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.ivCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(note.getIs_scrawl() == 0){//当前未收藏-->收藏--》收藏失败恢复图标
                    binding.ivCollect.setImageResource(R.mipmap.btn_collect);
                    viewModel.notepadCollect(note.getNotepad_type_id(),1).observe(NoteDetailActivity.this, new Observer<BaseResponseEntity>() {
                        @Override
                        public void onChanged(BaseResponseEntity baseResponseEntity) {
                            if(baseResponseEntity.getCode() != 200){
                                binding.ivCollect.setImageResource(R.mipmap.btn_uncollect);
                                ToastUtils.showShort("收藏失败");
                            }else{
                                ToastUtils.showShort("收藏成功");
                            }

                        }
                    });
                }else{
                    binding.ivCollect.setImageResource(R.mipmap.btn_uncollect);
                    viewModel.notepadCollect(note.getNotepad_type_id(),0).observe(NoteDetailActivity.this, new Observer<BaseResponseEntity>() {
                        @Override
                        public void onChanged(BaseResponseEntity baseResponseEntity) {
                            if(baseResponseEntity.getCode() != 200){
                                binding.ivCollect.setImageResource(R.mipmap.btn_collect);
                                ToastUtils.showShort("收藏失败");
                            }else{
                                ToastUtils.showShort("收藏成功");
                            }
                        }
                    });
                }
            }
        });

        binding.ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(position == mList.size()-1){
                    position = 0;
                    note = mList.get(position);
                }else{
                    position++;
                    note = mList.get(position);
                }
                showInfo();
            }
        });
        binding.ivPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(position == 0){
                    position = mList.size()-1;
                    note = mList.get(position);
                }else{
                    position--;
                    note = mList.get(position);
                }
                showInfo();
            }
        });
        mNoteDetailImageAdapter.setItemChildClickListener(new NoteTypeEditAdapter.ItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("note",note);
                startActivityBundle(ImageDetailActivity.class,bundle);
            }
        });
        binding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.notepadSubclassDel(note.getNotepad_type_id()).observe(NoteDetailActivity.this, new Observer<BaseResponseEntity>() {
                    @Override
                    public void onChanged(BaseResponseEntity baseResponseEntity) {
                        if(baseResponseEntity.getCode() == 200){
                            ToastUtils.showShort("删除成功");
                            finish();
                            Event event = new Event();
                            event.type = Event.NOTE_DELETE;
                            event.target = "NoteFragment";
                            event.data = position;
                            EventBus.getDefault().post(event);
                        }

                    }
                });
            }
        });



    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(NoteDetailViewModel.class);
    }
}